package com.hopu.product.dao.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.hopu.product.dao.ProductDao;
import com.hopu.product.domain.Product;
import com.hopu.product.util.DBUtil;

public class ProductDaoImpl implements ProductDao {

	private PreparedStatement pst;
	private ResultSet rs;
	private Product p;
	@Override
	public List<Product> findAll() {
		List<Product> list = new ArrayList<Product>();
		String sql = "select pid,pname,ptype,price,pnum,content from product";
		pst = DBUtil.getPst(sql);
		try {
			rs = pst.executeQuery();
			while(rs.next()) {
				p = new Product(rs.getInt(1), rs.getString(2), rs.getString(3),
						rs.getInt(4), rs.getInt(5), rs.getString(6));
				list.add(p);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			DBUtil.close(rs);
		}
		return list;
	}
	@Override
	public Product findById(Product product) {
		String sql = "select pid,pname,ptype,price,pnum,content "
				+ "from product where pid=?";
		pst = DBUtil.getPst(sql);
		try {
			pst.setInt(1, product.getPid());
			rs = pst.executeQuery();
			while(rs.next()) {
				p = new Product(rs.getInt(1), rs.getString(2), rs.getString(3),
						rs.getInt(4), rs.getInt(5), rs.getString(6));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			DBUtil.close(rs);
		}
		return p;
	}
	
	@Override
	public int add(Product p) {
		int num = -1;
		String sql = "insert into product(pname,ptype,price,pnum,content) "
				+ "  values(?,?,?,?,?)";
		pst=DBUtil.getPst(sql);
		try {
			pst.setString(1, p.getPname());
			pst.setString(2, p.getPtype());
			pst.setInt(3, p.getPrice());
			pst.setInt(4, p.getPnum());
			pst.setString(5, p.getContent());
			num = pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			DBUtil.close(rs);
		}
		return num;
	}
	@Override
	public int update(Product p) {
		int num = -1;
		String sql = "update product set pname=?,ptype=?,price=?,pnum=?,content=? where pid=?";
		pst=DBUtil.getPst(sql);
		try {
			pst.setString(1, p.getPname());
			pst.setString(2, p.getPtype());
			pst.setInt(3, p.getPrice());
			pst.setInt(4, p.getPnum());
			pst.setString(5, p.getContent());
			pst.setInt(6, p.getPid());
			num = pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			DBUtil.close(rs);
		}
		return num;
	}
	@Override
	public int remove(Product product) {
		int num = -1;
		String sql = "delete from product where pid=?";
		pst=DBUtil.getPst(sql);
		try {
			pst.setInt(1, product.getPid());
			num = pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			DBUtil.close(rs);
		}
		return num;
	}
	@Override
	public int removes(int[] ids) {
		int num = -1;
		// 拼接,根据数组长度，生成对应得？
		String id="";
		for(int i=1;i<ids.length;i++){
			id+=",?";
		}
		String sql = "delete from product where pid in (?"+id+")";
		
		pst=DBUtil.getPst(sql);
		try {
			// 根据数组长度来给占位符赋值
			for(int i=0;i<ids.length;i++){
				pst.setInt(i+1, ids[i]);
			}
			num += pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			DBUtil.close(rs);
		}
		return num;
	}

}
